﻿using System;
using Framework.Core;
using Xunit;

namespace Framework.Tests.Core
{
    extern alias castlecore;
    public class ProxyInterceptorsTest
    {
        private static readonly castlecore::Castle.DynamicProxy.ProxyGenerator ProxyGenerator = new castlecore::Castle.DynamicProxy.ProxyGenerator();
        public interface IContract
        {
            void JustDoIt();
        }

        class FakeInterception : castlecore::Castle.DynamicProxy.IInterceptor
        {
            public void Intercept(castlecore::Castle.DynamicProxy.IInvocation invocation)
            {
                throw new NotImplementedException();
            }
        }

        class OtherInterception : castlecore::Castle.DynamicProxy.IInterceptor
        {
            public void Intercept(castlecore::Castle.DynamicProxy.IInvocation invocation)
            {
                throw new NotImplementedException();
            }
        }

        [Fact]
        public void Should_get_interceptor()
        {
            var proxy = GetProxy();
            Assert.NotNull(proxy.Get<FakeInterception>());
        }

        private static IContract GetProxy()
        {
            return ProxyGenerator.CreateInterfaceProxyWithoutTarget<IContract>(new FakeInterception());
        }

        [Fact]
        public void Should_throw_proxy_exception_when_it_is_not_proxy()
        {
            Assert.Throws<ProxyException>(() => new object().Get<FakeInterception>());
        }

        [Fact]
        public void Should_throw_proxy_exception_when_it_is_not_right_proxy()
        {
            var proxy = GetProxy();
            Assert.Throws<ProxyException>(() => proxy.Get<OtherInterception>());
        }
    }
}
